import api from './index'

// 作业相关接口
export interface Assignment {
  id: number
  title: string
  description: string
  class_id: number
  class_name: string
  problem_ids: string
  due_date: string
  total_score: number
  status: string
  created_at: string
  updated_at: string
  submission_count: number
}

export interface AssignmentDetail extends Assignment {
  problems: any[]
}

export interface CreateAssignmentRequest {
  title: string
  description?: string
  class_id: number
  problem_ids: number[]
  due_date: string
  total_score?: number
}

export interface UpdateAssignmentRequest {
  title?: string
  description?: string
  problem_ids?: number[]
  due_date?: string
  total_score?: number
  status?: string
}

export interface SubmitAssignmentRequest {
  problem_id: number
  code: string
  language?: string
}

export interface GradeSubmissionRequest {
  score: number
  feedback?: string
}

export const assignmentsApi = {
  // 获取作业列表
  getAssignments() {
    return api.get<{ code: number; message: string; data: Assignment[] }>('/assignments/')
  },

  // 获取作业详情
  getAssignmentDetail(assignmentId: number) {
    return api.get<{ code: number; message: string; data: AssignmentDetail }>(`/assignments/${assignmentId}`)
  },

  // 创建作业
  createAssignment(data: CreateAssignmentRequest) {
    return api.post<{ code: number; message: string; data: Assignment }>('/assignments/', data)
  },

  // 更新作业
  updateAssignment(assignmentId: number, data: UpdateAssignmentRequest) {
    return api.put<{ code: number; message: string; data: Assignment }>(`/assignments/${assignmentId}`, data)
  },

  // 提交作业
  submitAssignment(assignmentId: number, data: SubmitAssignmentRequest) {
    return api.post<{ code: number; message: string; data: any }>(`/assignments/${assignmentId}/submissions`, data)
  },

  // 获取作业提交记录
  getAssignmentSubmissions(assignmentId: number) {
    return api.get<{ code: number; message: string; data: any[] }>(`/assignments/${assignmentId}/submissions`)
  },

  // 批改作业提交
  gradeSubmission(submissionId: number, data: GradeSubmissionRequest) {
    return api.post<{ code: number; message: string; data: any }>(`/assignments/submissions/${submissionId}/grade`, data)
  },

  // 获取作业统计信息
  getAssignmentStats(assignmentId: number) {
    return api.get<{ code: number; message: string; data: any }>(`/assignments/${assignmentId}/stats`)
  }
}
